//
//  NotifyController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/23.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
//通知列表
class NotifyController: BaseController {
    var notifyTable:UITableView!//数据表格
    var notifyData:[NotifyModel]!
    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupNavBar(title: "通知", isback: true, appearWay: 2, isright: false)//自定义导航
        notifyData = Array<NotifyModel>()
        notifyTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH,width:kScreenW,height:kScreenH - kStatusBarH - kNavigationBarH),style: UITableViewStyle.plain)
        notifyTable?.delegate = self
        notifyTable?.dataSource = self
        notifyTable?.separatorStyle = UITableViewCellSeparatorStyle.none
        notifyTable.backgroundColor = tableBgColor
        notifyTable.rowHeight = UITableViewAutomaticDimension
        notifyTable.estimatedRowHeight = 120.0
        self.view.addSubview(notifyTable)
        if  reachability.isReachable{
            loadNotifyData()
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
extension NotifyController{
    //查询通知列表
    func loadNotifyData() {
        let urlString = "\(projectUrl)/notify/list/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)" //网络请求地址
        Alamofire.request(urlString, method: .get, parameters: nil, headers : headers).responseJSON { (response) in
            if response.result.isSuccess{
                let json = JSON(response.result.value as! [String : AnyObject])
                if json["success"] == true {
                    for (_,notifyJson) : (String,JSON) in json["rdata"] {
                        let model = NotifyModel.init(dict: notifyJson)
                            self.notifyData.append(model)
                    }
                    self.notifyTable.reloadData()
                }
                else{
                    self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                }
            }
            else{
                print(response.result.error!) //输出错误日志
            }
        }
    }
}
extension NotifyController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notifyData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
       var infoCell = tableView.dequeueReusableCell(withIdentifier: "notifyCellID") as? NotifyCell
        if infoCell == nil {
            infoCell = NotifyCell(style: UITableViewCellStyle.default,reuseIdentifier: "notifyCellID")
        }
        if notifyData.count > 0 {
            let model = notifyData[(indexPath as NSIndexPath).row]
            infoCell?.configCellDateWithModel(model)
        }
        return infoCell!
    }
}
